<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\question;

use addons\cloud_market\controller\api\CloudMarketApiController;
use addons\cloud_market\model\CloudMarketQuestion;
use addons\cloud_market\model\CloudMarketQuestionAnswer;

class Answer extends CloudMarketApiController
{
    protected $notApiCheck = ['list'];
    protected $notApiLogin = ['list'];
    /**
     * @var CloudMarketQuestionAnswer
     */
    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new CloudMarketQuestionAnswer();
    }

    //获取回复列表
    public function list()
    {
        $mix = [];
        $params = request()->buildParams();
        $params['order'] = "accept_switch DESC ,id desc";
        $res = $this->model->getPageList($mix,$params);
        $data = $res['data'];
        return $this->success($res['count']??0, $data, $this->model->getLastSql());
    }

    // 获取一条
    public function show()
    {
        $id = input('id');
        $data = $this->model->where('id', $id)->append(['user'])->find();
        return $this->success('', $data);
    }

    // 删除回复
    public function del()
    {
        $id = input('id');
        $user = $this->getUser();
        $answer = $this->model->where('id', $id)->find();
        if ($answer['user_id'] == $user['id'] || $user['id'] == 1) {
            // 判断被删除的回答是否有子回答
            $child_answer = $this->model->where('parent_id', $id)->find();
            if ($child_answer) {
                return $this->error('该回答下有子回答，不能删除');
            }
            // 删除回答
            $answer->delete();
        }
        return $this->success('ok');
    }

    public function submit()
    {
        $user = $this->getUser();
        $question_id = input('post.question_id');
        $reply_user_id = input('post.reply_user_id', 0);
        $question = (new CloudMarketQuestion())->where('id', $question_id)->find();
        $data = array(
            'user_id'       => $user['id'],
            'question_id'   => $question_id,
            'answer_id'     => input('post.answer_id', 0),
            'parent_id'     => input('post.parent_id', 0),
            'reply_user_id' => $reply_user_id,
            'is_comment'    => input('post.is_comment', 0), // 0回答问题 1评论回答
            'content'       => input('post.content', '', 'htmlspecialchars'),
        );
        $this->model->save($data);
        if ($user['id'] != $reply_user_id) {
            if ($data['is_comment'] == 0) {
                // 如果是回答问题
                $reply_user_id = $question['user_id'];
            }
            $content = mb_substr(input('post.content'), 0, 200);
            unset($data['content']);
            // TODO 发送消息
//            (new Message())->addAnswerMessage($reply_user_id, $data['user_id'], $content, $data);
        }
        return $this->success('提交成功', $this->model);
    }
}
